import Vue from 'vue'
import Vuex from 'vuex'
import ajax from '@/plugins/ajax'
import { syncVars, syncVarCallback } from '@/env'
import { isEmpty } from '@/utils/is'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    mountStatus: 0,
    token: '',
    isSnapshotuser: 0,
    wxRedirectState: '',
    config: {},
    category: [],
    userInfo: {}
  },
  mutations: {
    setState(state, data = {}) {
      Object.keys(data).forEach(key => {
        state[key] = data[key]
        if (syncVars.includes(key) && !isEmpty(data[key])) {
          const callbackName = syncVarCallback(key)
          if (Vue[callbackName]) Vue[callbackName](data[key])
        }
      })
    }
  },
  actions: {
    getUserInfo(context, payload) {
      ajax.post('api/userInfo').then(res => {
        if (res.code === 0) {
          context.commit('setState', { userInfo: res.data })
        }
      })
    }
  }
})
export default store
